<template>
	<view class="pad10">
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item class="line-bottom" label="车型" required name="carId">
				<button class="fxl orange-btn" style="width:50%" type="primary" plain="true" @click="startOcr">
					<i class="iconfont icon-paizhao"></i>OCR识别车型
				</button>
				<span class="fxl" style="width:10%;text-align: center;line-height:30px;">或</span>
				<button class="fxl orange-btn" style="width:40%" type="primary" plain="true"
					@click="selectPinpChexChex">
					<i class="iconfont icon-sousuo"></i>手动选择
				</button>
				<div class="clear h10"></div>
				<text>{{formData.carFullName}}</text>
				<text v-if="formData.carPrice" decode="true">(厂商指导价: {{formData.carPrice}}&nbsp;万)</text>
			</uni-forms-item>
			<uni-forms-item label="注册日期" required name="chudrq">
				<text class="data-font clearfix" @click="open">{{formData.chudrq}}</text>
			</uni-forms-item>
			<uni-forms-item label="行驶里程" required name="xingslc">
				<uni-easyinput type="number" v-model="formData.xingslc" placeholder="请输行驶里程 (公里)" />
			</uni-forms-item>
			<uni-forms-item label="所在城市" required>
				<uni-row>
					<uni-col :span="18">
						<!-- <picker @change="bindPickerChange" :value="cityIndex" :range="toufdqList" range-key="itemText">
							<view style="padding-top: 8px">{{ toufdqList[cityIndex].itemText == "" ? "点击选择" : toufdqList[cityIndex].itemText}}</view>
						</picker> -->
						<uni-data-picker :localdata="cityList" popup-title="请选择所在城市" @change="onchange"
							v-model="selectedCity"></uni-data-picker>
					</uni-col>
					<uni-col :span="6">
						<button type="warn" size="mini" @click="ziddw()">定位</button>
					</uni-col>
				</uni-row>
			</uni-forms-item>

			<uni-forms-item label="产品" required>
				<radio-group @change="radioChange2" v-model="formData.level2Sku">
					<label class="uni-list-cell pos-rel uni-list-cell-pd margin5" v-for="(item, index) in level2SkuList"
						:key="index">
						<view class="fxl btn-radio wid120 fs14" :class="index===current2?'focus':''">
							<radio v-show=false :value="item.skuvalue" />{{item.skuname}}
						</view>
						<button type="warn" class="fxr margin10T" size="mini"
							@click="productDetail(item.skuvalue)">详情</button>
					</label>
				</radio-group>
			</uni-forms-item>

			<!-- <uni-forms-item label="具体规格">
				<radio-group @change="radioChange3" v-model="formData.level3Sku">
					<label class="uni-list-cell  uni-list-cell-pd margin5" v-for="(item, index) in level3SkuList"
						:key="index">
						<view class="btn-radio fs14" :class="index===current3?'focus':''">
							<radio v-show=false :value="item.skuvalue" />{{item.skuname}}
						</view>
					</label>
				</radio-group>
			</uni-forms-item> -->

			<uni-forms-item label="购买年限" required>
				<radio-group @change="radioChange4" v-model="formData.level4Sku">
					<label class="uni-list-cell  uni-list-cell-pd margin5" v-for="(item, index) in level4SkuList"
						:key="index">
						<view class="btn-radio fs14" :class="index===current4?'focus':''">
							<radio v-show="false" :value="item.policyTimeMileage" :checked="index === current4" />
							{{item.goumnxName}}
						</view>
					</label>
				</radio-group>
			</uni-forms-item>

			<!-- 改成赔付率体现在产品中 -->
			<!-- <uni-forms-item label="赔付率" required>
				<radio-group @change="radioChange5" v-model="formData.level5Sku">
					<label class="uni-list-cell  uni-list-cell-pd margin5" v-for="(item, index) in level5SkuList" :key="index">
						<view class="btn-radio fs14" :class="index===current4?'focus':''">
							<radio v-show="false" :value="item.vallue" :checked="index === current4" />{{item.name}}
						</view>
					</label>
				</radio-group>
			</uni-forms-item> -->

		</uni-forms>


		<div class="clear h20"></div>
		<!-- <block v-if="isShowBindButton == true">
			<button class="btn-red" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">立 即 报 价</button>
		</block>
		<block v-else>
			<button class="btn-red" @click="submitForm()">立即报价</button>
		</block> -->
		<button class="btn-red" @click="submitForm()">立即报价</button>

		<uni-calendar ref="calendar" :insert="false" @confirm="confirm" />

		<!-- 分割线 -->

		<u-keyboard mode="car " v-model="show4car" :mask="false" @change="valChange2" @backspace="backspace2">
		</u-keyboard>

		<uni-card class="font-bold-title" title="核心保障" mode="title" :extra="hexbz" note="Tips" :isShadow="true">
			<uni-row class="block-margin">
				<uni-col :span="12">
					赔付限额
				</uni-col>
				<uni-col :span="12">
					<view class="clearfix text-right" decode="true">
						{{carInfo.price == null ? "" : carInfo.price * product.peifl}}&nbsp;万
					</view>
				</uni-col>
			</uni-row>


			<template v-slot:footer>
				<view>
					<uni-row>
						<uni-col :span="12" class="marign10T">
							售价
						</uni-col>
						<uni-col :span="12">
							<view class="clearfix text-right" decode="true">
								<span class="font-orange-num">{{feilb.premium}}&nbsp;元</span>
							</view>
						</uni-col>
					</uni-row>
				</view>
			</template>
		</uni-card>

		<!-- <uni-card class="font-bold-title" title="升级保障" mode="title" :isShadow="true">
			<radio-group @change="radioChange">
				<label class="uni-list-cell pos-rel uni-list-cell-pd margin5" v-for="(item, index) in beixList"
					:key="index">
					<view class="fxl btn-radio wid120 fs14" :class="index===current?'focus':''">
						<radio v-show=false :value="item.premium" />{{item.chanpmc}}
					</view>
					<button type="warn" class="fxr margin10T"  size="mini" @click="productDetail(item.productId)">详情</button>
				</label>
			</radio-group>
		</uni-card> -->

		<uni-section class="pub-title" title="车辆信息" type="line"></uni-section>
		<uni-card :isShadow="true">
			<uni-forms-item label="车牌号" required>
				<uni-easyinput type="text" v-model="formData.cheph" placeholder="请输入车牌号" :inputBorder="true"
					@iconClick="showKeyboard('car')" suffixIcon="gear" />
			</uni-forms-item>
		</uni-card>

		<uni-card title="会员福利" class="card-block" v-if="taocList.length > 0" :isShadow="true">
			<ul class="icon-list-four" v-for="(item,index) in taocList" :key="index">
				<li>
					<!-- <i :class="item.tub"></i> -->
					<center>
						<image :src="item.tub" class="img"></image>
					</center>
					<p>{{item.xiangmmc}}</p>
					<p>{{item.shuom}}</p>
				</li>
			</ul>
		</uni-card>


		<view style="color: #ffffff;">
			<p>1</p>
		</view>

		<button @click="zhifu" class="btn-red bottom-btn border-no">支付</button>

	</view>
</template>

<script>
	import moment from "moment"

	export default {
		data() {
			return {
				selectedCity: "",
				cityList: [],
				hexbz: null,
				beixList: null,
				current: 0,
				show4car: false,
				baseInfo: uni.getStorageSync("baseInfo"),
				carInfo: uni.getStorageSync("carInfo"),
				feilb: {
					premium: ""
				},
				product: null,

				// 分割线
				goumnx2: null,
				goumnxIndex: 0,
				goumnxList: [],
				cityIndex: uni.getStorageSync("cityIndex") ? uni.getStorageSync("cityIndex") : 0,
				toufdqList: [],
				current4: 0,
				current3: 0,
				current2: 0,
				temp: {},
				taocList: [],
				isShowBindButton: false,
				current: 0,
				levelSkuList: [],
				level2SkuList: [],
				level3SkuList: [],
				level4SkuList: [],
				level5SkuList: [{
						name: "赔付50%",
						value: 50
					},
					{
						name: "赔付100%",
						value: 100
					},
				],
				formData: {
					cheph: "",
					carFullName: "",
					carId: "",
					chudrq: "点击选择注册日期 >",
					xingslc: "",
					goumnx: "",
					level2Sku: null
				},
				rules: {}
			}
		},

		onUnload() {
			wx.switchTab({
				url: '/pages/home/home'
			})
		},

		onLoad() {
			let that = this
			// uni.setStorageSync("baseInfo", {})
			// that.loginByOpenId()

			// 购买年限
			// uni.request({
			// 	url: getApp().globalData.serviceUrl + "/sys/dictItem/list",
			// 	data: {
			// 		dictId: "1438418148669345794",
			// 	},
			// 	dataType: 'json',
			// 	method: 'GET',
			// 	success: (result) => {
			// 		that.goumnxList = result.data.result.records
			// 	},
			// })

			// 投放地区
			// uni.request({
			// 	url: getApp().globalData.serviceUrl + "/sys/dictItem/list",
			// 	data: {
			// 		dictId: "1435808345820295170",
			// 	},
			// 	dataType: 'json',
			// 	method: 'GET',
			// 	success: (result) => {
			// 		if (result.data.success) {
			// 			that.toufdqList = result.data.result.records

			// 			let test = null
			// 			try {
			// 				test = that.toufdqList[that.cityIndex].itemValue

			// 			} catch(error) {
			// 				console.error(error)
			// 				that.cityIndex = 0
			// 				test = that.toufdqList[that.cityIndex].itemValue

			// 			}

			// 			that.loadLevel2SkuList(test)

			// 			let baseInfo = uni.getStorageSync("baseInfo")
			// 			if(!baseInfo)
			// 				baseInfo = {}

			// 			baseInfo.level1Sku = that.toufdqList[that.cityIndex].itemValue
			// 			baseInfo.level1Sku_fy = that.toufdqList[that.cityIndex].itemText
			// 			uni.setStorageSync("baseInfo", baseInfo)

			// 		}
			// 	}
			// });

			// 投放城市列表
			uni.request({
				url: getApp().globalData.serviceUrl + "/order/kuybOrder/getShengsqxData2",
				data: {},
				header: {
					"X-Access-Token": uni.getStorageSync("token")
				},
				method: 'GET',
				success: (resp) => {
					if (resp.data.success) {
						that.cityList = resp.data.result

					} else {
						that.loginByOpenId()

						uni.showToast({
							title: "链接服务器失败，请稍后重试！",
							icon: 'none',
							duration: 3000
						});
					}
				}
			})

			this.selectedCity = uni.getStorageSync("selectedCity")
			if (this.selectedCity) {
				that.loadLevel2SkuList(this.selectedCity)

				let baseInfo = uni.getStorageSync("baseInfo")
				if (!baseInfo)
					baseInfo = {}

				baseInfo.level1Sku = this.selectedCity
				uni.setStorageSync("baseInfo", baseInfo)
			}

		},

		onShow() {
			this.current4 = 0
			this.current3 = 0
			this.current2 = 0

			if (uni.getStorageSync("carInfo").fullName != null) {
				let carInfo = uni.getStorageSync("carInfo")
				this.formData.carFullName = carInfo.fullName
				this.formData.carPrice = carInfo.price
				this.formData.carId = carInfo.carid
				this.formData.cheph = carInfo.cheph

				this.formData.chudrq = moment(carInfo.chudrq).format("YYYY-MM-DD")
				this.formData.xingslc = uni.getStorageSync("baseInfo").xingslc

				let test = null
				try {
					if (this.toufdqList != null && this.toufdqList.length > 0) {
						test = this.toufdqList[this.cityIndex].itemValue
						this.loadLevel2SkuList(test)
					}

				} catch (error) {
					console.error(error)
					this.cityIndex = 0
					test = this.toufdqList[this.cityIndex].itemValue

				}

			}
		},

		methods: {
			onchange(e) {
				const value = e.detail.value
				this.selectedCity = value[value.length - 1].value
				uni.setStorageSync("selectedCity", this.selectedCity)

				this.loadLevel2SkuList(this.selectedCity)

				let baseInfo = uni.getStorageSync("baseInfo")
				if (!baseInfo)
					baseInfo = {}

				baseInfo.level1Sku = this.selectedCity
				uni.setStorageSync("baseInfo", baseInfo)
			},

			loginByOpenId() {
				let that = this
				let openId = uni.getStorageSync("openId").openid

				uni.request({
					url: getApp().globalData.serviceUrl + "/sys/loginByOpenId",
					data: {
						openId: openId
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							that.isGetPhoneNumber = false
							uni.setStorageSync("token", result.data.result.token)
							uni.setStorageSync("userInfo", result.data.result)

						} else {
							that.isGetPhoneNumber = true
						}
					}
				});
			},

			payment() {
				let that = this

				uni.requestSubscribeMessage({
					tmplIds: ['LWpG_Z7ky9l4qW5N9VaCcoVhlfuS18PY_q_MF3oIscw'],
					success(res) {
						uni.request({
							url: getApp().globalData.serviceUrl + "/weixin/getPrepayIdAndPaySign",
							data: {
								dingdbh: uni.getStorageSync("selectedOrder").dingdbh,
								productName: uni.getStorageSync("product").chanpmc,
								dingdje: uni.getStorageSync("selectedOrder").dingdje,
								openId: uni.getStorageSync("userInfo").openId
							},
							header: {
								"X-Access-Token": uni.getStorageSync("token")
							},
							method: 'POST',
							dataType: 'json',
							success: (resp) => {
								if (resp.data.success) {
									uni.requestPayment({
										provider: 'wxpay',
										timeStamp: resp.data.result.timeStamp,
										nonceStr: resp.data.result.nonceStr,
										package: 'prepay_id=' + resp.data.result.prepay_id,
										signType: 'RSA',
										paySign: resp.data.result.paySign,

										// timeStamp: "1632321294",
										// nonceStr: "97d92c48c4ec432eaaee00ee802f6b66",
										// package: "prepay_id=wx22223452765071c4b0b27aae5c09300000",
										// paySign: "WAF+E9mwrKufJsim5hA+ybqILBZNTZzz9Lnc6Oom/DIrTQ/bqwYNxG9VTTEeLgPeFDPLgAOedruIKRtr84RFfGFx0u+nm36pV1xq3es2aLlQh3LAje4SajFe/vzRB82Gqn1oTG6oTI0/jZs6OZ8u4SLoB2XxB0mrCu5T+ilQ6VwS4rRRzY3VGy9Y4XMzYXAjhFPel+0iSjnQdo8tyhDUSJb8UMm9U02khNKivXODF7QQ6YC2bOj442eOZ7I8yp586eOJAFzX6UnQ2kf3lPz6VrFZGl0iHnPyLU9ShZzLMJ0NyZgnGFSlFXf80xQLUuzKaq2lGDLwaAGaZJeyAF/89A==",
										// signType: 'RSA',

										success: function(res) {
											console.log('success:' + JSON.stringify(res));
											uni.navigateTo({
												url: '../../pages2/toubrxx/toubrxx'
											})

										},
										fail: function(err) {
											console.log('fail:' + JSON.stringify(err));
										}
									});

								} else {
									uni.showToast({
										title: resp.data.message,
										icon: 'none'
									});
								}
							}
						})
					}
				})
			},

			showKeyboard(type) {
				if (type == "car")
					this.show4car = true
			},

			// 按键被点击(点击退格键不会触发此事件)
			valChange2(val) {
				// 将每次按键的值拼接到value变量中，注意+=写法
				this.formData.cheph += val;
				console.log(this.value);
			},
			// 退格键被点击
			backspace2() {
				// 删除value的最后一个字符
				if (this.formData.cheph.length)
					this.formData.cheph = this.formData.cheph.substr(0, this.formData.cheph.length - 1);

			},

			zhifu() {
				let that = this

				if (!this.feilb || !this.feilb.premium) {
					uni.showToast({
						title: '先算费后支付!',
						icon: 'none'
					});
					return
				}

				if (!this.formData.cheph) {
					uni.showToast({
						title: '车牌号必填!',
						icon: 'none'
					});
					return
				}

				let carInfo = uni.getStorageSync("carInfo")
				carInfo.cheph = this.formData.cheph
				uni.setStorageSync("carInfo", carInfo)

				uni.showLoading({
					title: '加载中...'
				});
				uni.request({
					url: getApp().globalData.serviceUrl + "/order/kuybOrder/add",
					data: {
						dingdje: this.feilb.premium * 100,
						// dingdje: 1,
						openId: uni.getStorageSync("openId").openid,
						kuybOrderChanpList: [{
							chanpid: that.product.id,
							chanpmc: that.product.chanpmc,
							chanpzt: that.product.chanpzt,
							chanpms: that.product.chanpms,
						}],
						kuybOrderPolicyList: [{
							level1Sku: uni.getStorageSync("baseInfo").level1Sku,
							level2Sku: uni.getStorageSync("baseInfo").level2Sku,
							level3Sku: uni.getStorageSync("baseInfo").level3Sku,
							level4Sku: uni.getStorageSync("baseInfo").level4Sku,
							bebxcph: uni.getStorageSync("carInfo").cheph,
							chudrq: uni.getStorageSync("baseInfo").chudrq,
							xingslc: uni.getStorageSync("baseInfo").xingslc,
							chelmc: uni.getStorageSync("baseInfo").carFullName,
							beibxryx: uni.getStorageSync("toubrxx").email,
							peifxe: uni.getStorageSync("carInfo").price * that.product.peifl,
							productId: that.product.id,
							openId: uni.getStorageSync("openId").openid,
							nengylx: uni.getStorageSync("carInfo").nengylx,
							carid: uni.getStorageSync("carInfo").id
						}]
					},
					dataType: 'json',
					method: 'POST',
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					success: (result) => {
						uni.hideLoading()

						if (result.data.success) {
							uni.setStorageSync("selectedOrder", result.data.result)
							that.payment()

						} else {
							uni.showToast({
								title: result.data.message,
								icon: 'none'
							});
						}
					}
				});

			},

			getChanpById(productId) {
				let that = this

				uni.request({
					url: getApp().globalData.serviceUrl + "/chanp/kuybChanp/queryById",
					data: {
						id: productId
					},
					method: 'GET',
					success: (resp) => {
						if (resp.data.success) {
							that.product = resp.data.result
						}
					}
				})
			},

			radioChange: function(evt, b) {
				for (let i = 0; i < this.beixList.length; i++) {
					if (this.beixList[i].premium == evt.detail.value) {
						this.current = i;
						this.feilb.premium = evt.detail.value

						this.getChanpById(this.beixList[i].productId)

						break;
					}
				}
			},

			queryGoumnxList(productId, carId) {
				let that = this

				if (that.formData.chudrq == "点击选择注册日期 >") {
					uni.showToast({
						title: "请选择初登日期",
						icon: 'none'
					});
					return
				}

				uni.showLoading({
					title: "查询购买年限..."
				})

				return new Promise(resolve => {
					uni.request({
						url: getApp().globalData.serviceUrl + "/feilb/kuybFeilb/getStandards",
						data: {
							productId: productId,
							carId: carId,
							chudrq: that.formData.chudrq,
							city: that.selectedCity
						},
						header: {
							"X-Access-Token": uni.getStorageSync("token")
						},
						dataType: 'json',
						method: 'GET',
						success: (resp) => {
							uni.hideLoading()
							if (resp.data.success) {
								that.current4 = 0
								that.level4SkuList = resp.data.result
								if (that.level4SkuList != null && that.level4SkuList.length > 0) {
									that.formData.level4Sku = that.level4SkuList[0].policyTimeMileage
									that.formData.warrantyPolicyTime = that.level4SkuList[0]
										.warrantyPolicyTime

									let baseInfo = uni.getStorageSync("baseInfo")
									if (baseInfo == "")
										baseInfo = {}

									baseInfo.level4Sku_fy = that.level4SkuList[0].goumnxName
									uni.setStorageSync("baseInfo", baseInfo)

								} else {
									that.formData.level4Sku = null

									let baseInfo = uni.getStorageSync("baseInfo")
									if (baseInfo != "") {
										baseInfo.level4Sku_fy = null
										uni.setStorageSync("baseInfo", baseInfo)
									}
								}

								resolve("查询结果完成")

							} else {
								uni.showToast({
									title: resp.data.message,
									icon: 'none'
								});
							}
						}
					})
				})
			},

			productDetail(productId) {
				uni.navigateTo({
					url: '../../pages2/productDetail/productDetail?productId=' + productId
				})
			},

			// 自动定位
			ziddw() {
				let that = this
				uni.request({
					url: "https://apis.map.qq.com/ws/location/v1/ip",
					data: {
						key: "UVHBZ-4CYKW-KEWRS-RYQMA-53EBF-FYBWS"
					},
					dataType: 'json',
					method: 'GET',
					success: (resp) => {
						if (resp.data.status == 0) {
							let city = resp.data.result.ad_info.city
							// uni.showToast({
							// 	title: '定位到当前城市: ' + city,
							// 	icon: 'none'
							// });

							let match = false
							// that.toufdqList.forEach((item, index) => {
							// 	if (city.search(item.itemText) >= 0) {
							// 		match = true
							// 		that.cityIndex = index
							// 		uni.setStorageSync("cityIndex", index)
							// 		that.loadLevel2SkuList(item.itemValue)

							// 		return
							// 	}
							// })

							that.cityList.forEach((item) => {
								item.children.forEach((item2) => {
									if (city.search(item2.text) >= 0) {
										match = true
										that.selectedCity = item2.value
										that.loadLevel2SkuList(that.selectedCity)

										let baseInfo = uni.getStorageSync("baseInfo")
										if (!baseInfo)
											baseInfo = {}

										baseInfo.level1Sku = that.selectedCity
										uni.setStorageSync("baseInfo", baseInfo)

										uni.setStorageSync("selectedCity", that.selectedCity)

										return
									}
								})
							})

							if (!match) {
								uni.showToast({
									title: "自动定位失败，请手工选择!",
									icon: 'none',
									duration: 3000
								})

							}

						}

					}
				});
			},

			// bindPickerChange: function(e) {
			// 	console.log('picker发送选择改变，携带值为', e.target.value)
			// 	this.cityIndex = e.target.value
			// 	uni.setStorageSync("cityIndex", e.target.value)

			// 	this.loadLevel2SkuList(this.toufdqList[e.target.value].itemValue)

			// 	let baseInfo = uni.getStorageSync("baseInfo")
			// 	baseInfo.level1Sku = this.toufdqList[e.target.value].itemValue
			// 	baseInfo.level1Sku_fy = this.toufdqList[e.target.value].itemText
			// 	uni.setStorageSync("baseInfo", baseInfo)
			// },

			// bindPickerChange2: function(e) {
			// 	console.log('picker发送选择改变，携带值为', e.target.value)
			// 	this.goumnxIndex = e.target.value
			// 	this.goumnx2 = this.goumnxList[this.goumnxIndex].itemValue
			// },

			getTaocList(productId) {
				let that = this

				uni.request({
					url: getApp().globalData.serviceUrl + "/taoc/kuybTaoc/list",
					data: {
						suoscp: productId,
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							that.taocList = result.data.result.records
							uni.setStorageSync("taocList", that.taocList)
						}
					}
				});
			},

			// loadLevel4SkuList(skuId) {
			// 	let that = this

			// 	uni.request({
			// 		url: getApp().globalData.serviceUrl + "/chanpSku/kuybChanpSku/list",
			// 		data: {
			// 			parentid: skuId,
			// 			column: "sequence",
			// 			order: "asc",
			// 			level: 3
			// 		},
			// 		dataType: 'json',
			// 		method: 'GET',
			// 		success: (result) => {
			// 			if (result.data.success) {
			// 				that.level4SkuList = result.data.result.records
			// 				if (that.level4SkuList != null && that.level4SkuList.length > 0) {
			// 					that.formData.level4Sku = that.level4SkuList[0].skuvalue

			// 					let baseInfo = uni.getStorageSync("baseInfo")
			// 					if(baseInfo == "")
			// 						baseInfo = {}

			// 					baseInfo.level4Sku_fy = that.level4SkuList[0].skuname
			// 					uni.setStorageSync("baseInfo", baseInfo)

			// 				} else {
			// 					that.formData.level4Sku = null

			// 					let baseInfo = uni.getStorageSync("baseInfo")
			// 					if(baseInfo != "") {
			// 						baseInfo.level4Sku_fy = null
			// 						uni.setStorageSync("baseInfo", baseInfo)
			// 					}
			// 				}

			// 			}
			// 		}
			// 	});
			// },

			loadLevel3SkuList(skuId) {
				let that = this

				uni.request({
					url: getApp().globalData.serviceUrl + "/chanpSku/kuybChanpSku/list",
					data: {
						parentid: skuId,
						column: "sequence",
						order: "asc",
						level: 2
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							that.level3SkuList = result.data.result.records
							if (that.level3SkuList != null && that.level3SkuList.length > 0) {
								that.formData.level3Sku = that.level3SkuList[0].skuvalue

								let baseInfo = uni.getStorageSync("baseInfo")
								if (baseInfo == "")
									baseInfo = {}
								baseInfo.level3Sku_fy = that.level3SkuList[0].skuname
								uni.setStorageSync("baseInfo", baseInfo)


								// that.loadLevel4SkuList(that.level3SkuList[0].id)

							} else {
								that.formData.level3Sku = null

								let baseInfo = uni.getStorageSync("baseInfo")
								if (baseInfo != "") {
									baseInfo.level3Sku_fy = null
									uni.setStorageSync("baseInfo", baseInfo)
								}

								// that.level4SkuList = []
							}

						}
					}
				});
			},

			loadLevel2SkuList(skuId) {
				let that = this

				if (!uni.getStorageSync("carInfo")) {
					uni.showToast({
						title: '请先选择车型！',
						icon: 'none'
					});
					return
				}

				if (!uni.getStorageSync("carInfo").nengylx) {
					uni.showToast({
						title: '能源类型异常！',
						icon: 'none'
					});
					return
				}

				uni.request({
					url: getApp().globalData.serviceUrl + "/chanpSku/kuybChanpSku/list",
					data: {
						parentid: skuId,
						column: "sequence",
						order: "asc",
						level: 1,
						nengylx: uni.getStorageSync("carInfo").nengylx,
						pinp: uni.getStorageSync("carInfo").pinp == undefined ? null : uni.getStorageSync(
							"carInfo").pinp,
						from: 1
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							that.level2SkuList = result.data.result.records
							if (that.level2SkuList != null && that.level2SkuList.length > 0) {
								that.formData.level2Sku = that.level2SkuList[0].skuvalue

								uni.setStorageSync("product", {
									chanpid: that.level2SkuList[0].skuvalue,
									chanpmc: that.level2SkuList[0].skuname
								})

								that.getChanpById(uni.getStorageSync("product").chanpid)

								let baseInfo = uni.getStorageSync("baseInfo")
								if (baseInfo == "")
									baseInfo = {}

								baseInfo.level2Sku_fy = that.level2SkuList[0].skuname
								uni.setStorageSync("baseInfo", baseInfo)


								// that.loadLevel3SkuList(that.level2SkuList[0].id)
								//this.loadLevel4SkuList(that.level2SkuList[0].id)
								this.queryGoumnxList(that.level2SkuList[0].skuvalue, that.formData.carId)

								that.getTaocList(that.level2SkuList[0].skuvalue)

							} else {
								that.formData.level2Sku = null

								let baseInfo = uni.getStorageSync("baseInfo")
								if (baseInfo != "") {
									baseInfo.level2Sku_fy = null
									uni.setStorageSync("baseInfo", baseInfo)
								}

								that.level3SkuList = []
								that.level4SkuList = []

								uni.showToast({
									title: '暂时不承保这个车型!',
									icon: 'none'
								});
							}

						}
					}
				});
			},

			suanf() {
				let that = this

				if (that.formData.carId == undefined || that.formData.carId == "") {
					uni.showToast({
						title: '请先选择车型！',
						icon: 'none'
					});
					return
				}

				if (that.formData.chudrq == "点击选择初登日期 >") {
					uni.showToast({
						title: '初登日期必选!',
						icon: 'none'
					});
					return
				}

				if (!that.formData.xingslc) {
					uni.showToast({
						title: '行驶里程必选!',
						icon: 'none'
					});
					return
				}

				if (!that.formData.level4Sku) {
					uni.showToast({
						title: '查询费率表失败!',
						icon: 'none'
					});
					return
				}

				let beixList = []
				uni.setStorageSync("beixList", beixList)

				// 备选列表
				// that.level2SkuList.forEach(element => {
				// 	uni.request({
				// 		url: getApp().globalData.serviceUrl + "/feilb/kuybFeilb/getPremiumByParam",
				// 		data: {
				// 			// 产品 id
				// 			productId: element.skuvalue,
				// 			// 车辆 id
				// 			carId: that.formData.carId,
				// 			// 初登日期
				// 			chudrq: that.formData.chudrq,
				// 			// 行驶里程
				// 			xingslc: that.formData.xingslc,
				// 			// 购买年限
				// 			goumnx: that.formData.level4Sku,
				// 			// 接口返回的原厂质保
				// 			warrantyPolicyTime: that.formData.warrantyPolicyTime
				// 		},
				// 		header: {
				// 			"X-Access-Token": uni.getStorageSync("token")
				// 		},
				// 		method: 'GET',
				// 		success: (resp) => {
				// 			if (resp.data.success) {
				// 				resp.data.result.chanpmc = element.skuname

				// 				let beixList = uni.getStorageSync("beixList")
				// 				beixList.push(resp.data.result)

				// 				uni.setStorageSync("beixList", beixList)
				// 			} 
				// 		}
				// 	})
				// })

				uni.showLoading({
					title: "算费中..."
				})
				uni.request({
					url: getApp().globalData.serviceUrl + "/feilb/kuybFeilb/getPremiumByParam",
					data: {
						// 产品 id
						productId: uni.getStorageSync("product").chanpid,
						// 车辆 id
						carId: that.formData.carId,
						// 初登日期
						chudrq: that.formData.chudrq,
						// 行驶里程
						xingslc: that.formData.xingslc,
						// 购买年限
						goumnx: that.formData.level4Sku,
						// 接口返回的原厂质保
						warrantyPolicyTime: that.formData.warrantyPolicyTime

					},
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					method: 'GET',
					success: (resp) => {
						uni.hideLoading()

						if (resp.data.success) {
							let carInfo = uni.getStorageSync("carInfo")
							carInfo.chudrq = that.formData.chudrq
							uni.setStorageSync("carInfo", carInfo)

							let baseInfo = uni.getStorageSync("baseInfo")
							const returnedTarget = Object.assign(baseInfo, that.formData);
							uni.setStorageSync("baseInfo", returnedTarget)
							uni.setStorageSync("feilb", resp.data.result)

							// uni.navigateTo({
							// 	url: '../../pages2/premium/premium'
							// })

							that.hexbz = uni.getStorageSync("baseInfo").level2Sku_fy + ", " + uni
								.getStorageSync("baseInfo").level4Sku_fy
							that.feilb.premium = resp.data.result.premium
							that.beixList = uni.getStorageSync("beixList")

						} else {
							that.loginByOpenId()

							uni.showToast({
								title: "链接服务器失败，请稍后重试！",
								icon: 'none',
								duration: 3000
							});
						}
					}
				})
			},

			loginByOpenId() {
				let that = this
				let openId = uni.getStorageSync("userInfo").openId

				uni.request({
					url: getApp().globalData.serviceUrl + "/sys/loginByOpenId",
					data: {
						openId: openId
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							that.isShowBindButton = false
							uni.setStorageSync("token", result.data.result.token)
							uni.setStorageSync("userInfo", result.data.result)

						} else {
							that.isShowBindButton = true
						}
					}
				});
			},

			getPhoneNumber(e) {
				let that = this

				uni.request({
					url: getApp().globalData.serviceUrl + "/weixin/bindMobile",
					data: {
						openId: uni.getStorageSync("openId").openid,
						sessionKey: uni.getStorageSync("openId").session_key,
						iv: e.detail.iv,
						encryptedData: e.detail.encryptedData,
						// nickName: that.temp.nickName,
						// avatarUrl: that.temp.avatarUrl
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							uni.setStorageSync("token", result.data.result.token)
							uni.setStorageSync("userInfo", result.data.result)

							that.suanf()
						}
					}
				});
			},

			selectPinpChexChex() {
				uni.navigateTo({
					url: '../../pages2/brandList/brandList'
				})
			},

			onnodeclick(node) {},

			startOcr() {
				uni.navigateTo({
					url: '../../pages2/takePicture/takePicture'
				})
			},

			submitForm() {
				let that = this

				this.$refs.form.validate().then(res => {
					this.suanf()

				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},

			open() {
				this.$refs.calendar.open();
			},
			confirm(e) {
				console.log(e);
				this.formData.chudrq = e.fulldate
			},
			
			// 同步查询购买年限结果
			async sync_queryGoumnxList(skuvalue, carId) {
				let rtn = await this.queryGoumnxList(skuvalue, carId)
				console.log("等待结果返回再调用下面", rtn)
				this.submitForm()
			},

			// 产品
			radioChange2: function(evt, b) {
				for (let i = 0; i < this.level2SkuList.length; i++) {
					if (this.level2SkuList[i].skuvalue === evt.detail.value) {
						this.current2 = i;
						this.formData.level2Sku = this.level2SkuList[i].skuvalue
						this.getTaocList(this.level2SkuList[i].skuvalue)

						uni.setStorageSync("product", {
							chanpid: this.level2SkuList[i].skuvalue,
							chanpmc: this.level2SkuList[i].skuname
						})

						this.getChanpById(uni.getStorageSync("product").chanpid)

						let baseInfo = uni.getStorageSync("baseInfo")
						baseInfo.level2Sku_fy = this.level2SkuList[i].skuname
						uni.setStorageSync("baseInfo", baseInfo)
						
						this.sync_queryGoumnxList(this.level2SkuList[i].skuvalue, this.formData.carId)

						break;
					}
				}
			},

			radioChange3: function(evt, b) {
				for (let i = 0; i < this.level3SkuList.length; i++) {
					if (this.level3SkuList[i].skuvalue === evt.detail.value) {
						this.current3 = i;
						this.formData.level3Sku = this.level3SkuList[i].skuvalue
						// this.loadLevel4SkuList(this.level3SkuList[i].id)

						let baseInfo = uni.getStorageSync("baseInfo")
						baseInfo.level3Sku_fy = this.level3SkuList[i].skuname
						uni.setStorageSync("baseInfo", baseInfo)

						break;
					}
				}
			},

			// 购买年限
			radioChange4: function(evt, b) {
				for (let i = 0; i < this.level4SkuList.length; i++) {
					if (this.level4SkuList[i].policyTimeMileage === evt.detail.value) {
						this.current4 = i;
						this.formData.level4Sku = this.level4SkuList[i].policyTimeMileage
						this.formData.warrantyPolicyTime = this.level4SkuList[i].warrantyPolicyTime

						let baseInfo = uni.getStorageSync("baseInfo")
						baseInfo.level4Sku_fy = this.level4SkuList[i].goumnxName
						uni.setStorageSync("baseInfo", baseInfo)

						break;
					}
				}

				this.submitForm()
			}

		}
	}
</script>

<style>

</style>
